package cn.edu.scnu.controller;

import cn.edu.scnu.entity.Comment;
import cn.edu.scnu.entity.Favourite;
import cn.edu.scnu.entity.User;
import cn.edu.scnu.service.*;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PlayerController {
    @Autowired
    private PlaybackHistoryService playbackHistoryService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private FavouriteService favouriteService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/player")
    public String player() {
        return "player";
    }

    @RequestMapping("/player/movieInfo")
    @ResponseBody
    public Map<String, Object> movieInfo(@RequestParam("movieId") Integer movieId) {
        Map<String, Object> response = new HashMap<>();
        User user = (User) redisTemplate.opsForValue().get("userLogin");
        if (user != null) {
            playbackHistoryService.addPlaybackHistory(user.getPhone(), movieId);
        }
        response.put("resMovie", movieService.findMovieById(movieId));
        response.put("resUser", redisTemplate.opsForValue().get("userLogin"));
        response.put("resDirectors", staffService.getDirectorsByMovieId(movieId));
        response.put("resActors", staffService.getActorsByMovieId(movieId));
        return response;
    }

    @RequestMapping("/player/editFavourite")
    @ResponseBody
    public String editFavourite(Integer movieId, HttpSession session) {
        User user = (User) session.getAttribute("userLogin");
        // 如果用户没有登录，则跳转到登录页
        if (user == null) {
            return "redirect:/toLogin";
        }
        String phone = user.getPhone();
        // 判断当前用户是否已收藏播放页的电影
        if (favouriteService.isFavourite(phone, movieId)) {
            // 若已收藏，则取消收藏
            favouriteService.deleteFavorite(phone, movieId);
            //session.removeAttribute("isFavourite");
        } else {
            // 若未收藏，则收藏该电影
            favouriteService.addFavourite(phone, movieId);
            //Favourite favourite = new Favourite();
            //favourite.setPhone(phone);
            //favourite.setMovieId(movieId);
            //session.setAttribute("isFavourite", favourite);
        }
        return "success";
    }

    // 根据是否已收藏来展示收藏按钮（不知道会不会用到，先写一下）
    @RequestMapping("/player/showIsFavourite")
    @ResponseBody
    public Map<String, Object> showIsFavourite(Integer movieId, HttpSession session, Model model) {
        Map<String, Object> response = new HashMap<>();
        // 判断用户是否已登录
        if (session.getAttribute("userLogin") != null) {
            User user = (User)session.getAttribute("userLogin");
            redisTemplate.opsForValue().set("userLogin", user);
            model.addAttribute("userLogin", user);
            // 判断用户是否已收藏播放页的电影
            if (favouriteService.isFavourite(user.getPhone(), movieId)) {
                // 已收藏
                Favourite favourite = new Favourite();
                favourite.setPhone(user.getPhone());
                favourite.setMovieId(movieId);
                //session.setAttribute("isFavourite", favourite);
                response.put("resFavourite", favourite);
            } else // 未收藏
                response.put("resFavourite", null);
        }
        return response;
    }

    @RequestMapping("/player/showComment")
    @ResponseBody
    public Map<String, Object> showComment(@RequestParam("movieId") Integer movieId, Model model) {
        Map<String, Object> response = new HashMap<>();
        response.put("resComment", commentService.findCommentByMovieId(movieId));
        return response;
//        List<Comment> commentList = commentService.findCommentByMovieId(movieId);
//        model.addAttribute("commentList", commentList);
//        return "player";
    }

    @RequestMapping("/player/addComment")
    @ResponseBody
    public String addComment(String commentContent, Integer movieId, HttpSession session) {
        User user = (User) session.getAttribute("userLogin");
        // 如果用户没有登录，则跳转到登录页
        if (user == null) {
            return "redirect:/toLogin";
        }
        commentService.addComment(commentContent, user.getPhone(), movieId);
        return "/player/showComment";
    }

}
